package com.siboxuetang.need.web.ctrl.wechat;

import com.alibaba.fastjson.JSONObject;
import com.siboxuetang.need.annotation.Public;
import com.siboxuetang.need.service.UserService;
import com.siboxuetang.need.service.WechatService;
import com.siboxuetang.need.util.ResultCode;
import com.siboxuetang.need.util.SHA1;
import com.siboxuetang.need.util.SessionUtils;
import com.siboxuetang.need.util.StringUtrils;
import com.siboxuetang.need.vo.NeedException;
import com.siboxuetang.need.vo.ResultBean;
import com.siboxuetang.need.vo.User;
import com.siboxuetang.need.vo.WechatUser;
import com.siboxuetang.need.web.BaseCtrl;
import com.siboxuetang.need.web.BaseWechatCtrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Create Time: 2018年04月26日 13:40
 *
 * @author fin
 */
@Controller
@RequestMapping("/wechat/validate")
public class WechatCtrl extends BaseWechatCtrl {

    @Autowired
    private WechatService wechatService;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    @Public
    @ResponseBody
    public String index(String echostr) {
        return echostr;
    }

    @RequestMapping("/sign")
    @ResponseBody
    public JSONObject sign(String url, HttpServletRequest request) {
        JSONObject json = new JSONObject();
                // 获取jsapi
                String jsApi = wechatService.getJsApiTicket();
                String noncestr = StringUtrils.UUID();
                String timestamp = String.valueOf(System.currentTimeMillis());

        json.put("nonceStr", noncestr);
        json.put("appid", wechatService.getAppId());
        json.put("timestamp", timestamp);
        json.put("url", url);
        StringBuilder sb = new StringBuilder();
        sb.append("jsapi_ticket")
                .append("=")
                .append(jsApi)
                .append("&noncestr")
                .append("=")
                .append(noncestr)
                .append("&timestamp")
                .append("=")
                .append(timestamp)
                .append("&url=")
                .append(url);
        json.put("signature", SHA1.encode(sb.toString()));
        debug(SHA1.encode(sb.toString()));

        return json;
    }

    @RequestMapping("/openid")
    @Public
    public String login(String code, HttpServletRequest request){

        String openid = this.wechatService.getUserOpenid(code);
        if(openid != null){
            ResultBean<User> userResultBean = this.userService.getUserByOpenid(openid, ORIGIN);

            debug("user login", userResultBean);

            // 获取用户信息成功
            if (userResultBean.getStatus() == ResultCode.SUCCESS) {
                debug("user login success");
                // session中储存用户信息
                SessionUtils.setUser(request, userResultBean.getData());
            } else if (userResultBean.getStatus() == ResultCode.NO_USER_INFO) {
                debug("no user, need register");
                // 可以为用户执行注册逻辑
                userResultBean = this.userService.registUser(openid, ORIGIN);
                SessionUtils.setUser(request, userResultBean.getData());
            }
        }else{
            throw new NeedException(ResultCode.LOGIN_FAIL, "登录失败");
        }
        return "redirect:/wechat/index";

    }


}
